#!/bin/bash

################################################################################
# 脚本名称: 1-build-base-image.sh
# 执行位置: 125测试服务器（有外网）
# 功能描述: 构建OpenJDK基础镜像 → 导出tar文件 → SCP传输到86服务器
# 作者: 高宇
################################################################################

set -e

# ============================================================================
# 配置参数
# ============================================================================

# 镜像信息
IMAGE_NAME="hydro-model-base"
IMAGE_TAG="openjdk17"
IMAGE_VERSION="1.0.0"

# 目标服务器信息（86服务器）
TARGET_SERVER="water@10.48.0.86"
TARGET_PATH="/home/water/docker/hydro-model"

# 导出文件名
EXPORT_FILE="${IMAGE_NAME}-${IMAGE_TAG}.tar"

# ============================================================================
# 颜色输出
# ============================================================================
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
BLUE='\033[0;34m'
NC='\033[0m'

print_info() {
    echo -e "${BLUE}[INFO]${NC} $1"
}

print_success() {
    echo -e "${GREEN}[SUCCESS]${NC} $1"
}

print_error() {
    echo -e "${RED}[ERROR]${NC} $1"
}

# ============================================================================
# 步骤1: 构建基础镜像
# ============================================================================
build_image() {
    print_info "=========================================="
    print_info "步骤1: 构建基础Docker镜像"
    print_info "=========================================="
    
    if [ ! -f "Dockerfile.base" ]; then
        print_error "Dockerfile.base 文件不存在！"
        exit 1
    fi
    
    print_info "开始构建镜像: ${IMAGE_NAME}:${IMAGE_TAG}"
    docker build -f Dockerfile.base -t ${IMAGE_NAME}:${IMAGE_TAG} .
    
    # 打版本标签
    docker tag ${IMAGE_NAME}:${IMAGE_TAG} ${IMAGE_NAME}:${IMAGE_VERSION}
    docker tag ${IMAGE_NAME}:${IMAGE_TAG} ${IMAGE_NAME}:latest
    
    print_success "镜像构建完成！"
    docker images | grep ${IMAGE_NAME}
}

# ============================================================================
# 步骤2: 导出镜像为tar文件
# ============================================================================
export_image() {
    print_info "=========================================="
    print_info "步骤2: 导出镜像为tar文件"
    print_info "=========================================="
    
    print_info "导出镜像到: ${EXPORT_FILE}"
    docker save -o ${EXPORT_FILE} ${IMAGE_NAME}:${IMAGE_TAG}
    
    # 显示文件大小
    FILE_SIZE=$(du -h ${EXPORT_FILE} | cut -f1)
    print_success "导出完成！文件大小: ${FILE_SIZE}"
}

# ============================================================================
# 步骤3: 传输tar文件到86服务器
# ============================================================================
transfer_image() {
    print_info "=========================================="
    print_info "步骤3: 传输tar文件到86服务器"
    print_info "=========================================="
    
    print_info "创建目标目录..."
    ssh ${TARGET_SERVER} "mkdir -p ${TARGET_PATH}"
    
    print_info "传输文件到: ${TARGET_SERVER}:${TARGET_PATH}"
    scp ${EXPORT_FILE} ${TARGET_SERVER}:${TARGET_PATH}/
    
    print_success "传输完成！"
}

# ============================================================================
# 步骤4: 清理本地tar文件
# ============================================================================
cleanup() {
    print_info "=========================================="
    print_info "步骤4: 清理本地tar文件"
    print_info "=========================================="
    
    print_info "删除本地tar文件: ${EXPORT_FILE}"
    rm -f ${EXPORT_FILE}
    
    print_success "清理完成！"
}

# ============================================================================
# 主流程
# ============================================================================
main() {
    echo ""
    echo "╔════════════════════════════════════════════════════════════════╗"
    echo "║                                                                ║"
    echo "║    [脚本1] 125服务器 - 构建基础镜像并传输到86服务器             ║"
    echo "║                                                                ║"
    echo "╚════════════════════════════════════════════════════════════════╝"
    echo ""
    
    print_info "镜像名称: ${IMAGE_NAME}:${IMAGE_TAG}"
    print_info "目标服务器: ${TARGET_SERVER}"
    print_info "目标路径: ${TARGET_PATH}"
    echo ""
    
    # 执行所有步骤
    build_image
    export_image
    transfer_image
    cleanup
    
    echo ""
    print_success "╔════════════════════════════════════════════════════════════════╗"
    print_success "║                    所有步骤执行完成！                            ║"
    print_success "╚════════════════════════════════════════════════════════════════╝"
    echo ""
    
    print_info "下一步操作："
    print_info "1. 登录到86服务器: ssh ${TARGET_SERVER}"
    print_info "2. 执行脚本2: cd ${TARGET_PATH} && bash 2-import-to-nexus.sh"
    echo ""
}

# 执行主流程
main

